﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for GioHangDAO
/// </summary>
public class GioHangDAO:AbstractDAO
{
	public GioHangDAO(String strPath)
        : base(strPath)
    {

    }
    ~GioHangDAO()
    {
        base.Dispose();
    }
    public DataSet GetItemsIntoGioHang(string id)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_GetItemsIntoGioHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Char, 36, id);

            base.FillDataSet(ref dsID, "GetItemsIntoGioHang");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }
    public DataSet GetTotalAmountGioHang(string id)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_GetTotalAmountGioHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Char, 36, id);

            base.FillDataSet(ref dsID, "GetTotalAmountGioHang");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }
    public Boolean AddItemToGioHang(GioHangDTO giohang)
    {
        try
        {
            base.sql = "usp_AddItemToGioHang";
            base.InitializeCommand();

            base.AddParameter("@id",
                SqlDbType.Char, 36, giohang.Id);

            base.AddParameter("@idSanPham",
                SqlDbType.Int, 100, giohang.IdSanPham);

            return ExecuteStoredProcedure();
        }
        catch (System.Exception ExceptionErr)
        {
            throw new System.Exception(ExceptionErr.Message,
            ExceptionErr.InnerException);
        }
    }

    public Boolean RemoveItemInGioHang(string id, int idSanPham)
    {
        try
        {
            base.sql = "usp_RemoveItemInGioHang";
            base.InitializeCommand();

            base.AddParameter("@id",
            SqlDbType.Char, 36, id);

            base.AddParameter("@idSanPham",
            SqlDbType.Int, 10, idSanPham);

            return ExecuteStoredProcedure();
        }
        catch (System.Exception ExceptionErr)
        {
            throw new System.Exception(ExceptionErr.Message,
            ExceptionErr.InnerException);
        }
    }

    public Boolean UpdateItemInGioHang(GioHangDTO shoppingCart)
    {
        try
        {
            base.sql = "usp_UpdateItemInGioHang";
            base.InitializeCommand();

            base.AddParameter("@id",
                SqlDbType.Char, 36, shoppingCart.Id);

            base.AddParameter("@idSanPham",
                SqlDbType.Int, 10, shoppingCart.IdSanPham);

            base.AddParameter("@SoLuong",
                SqlDbType.Int, 10, shoppingCart.SoLuong);

            return ExecuteStoredProcedure();
        }
        catch (System.Exception ExceptionErr)
        {
            throw new System.Exception(ExceptionErr.Message,
            ExceptionErr.InnerException);
        }
    }

    public DataSet CountOldCartsInGioHang(int days)
    {
        DataSet dsID;
        try
        {
            dsID = new DataSet();
            base.sql = "usp_CountOldCartsInGioHang";
            base.InitializeCommand();

            base.AddParameter("@Days",
            SqlDbType.Int, 10, days);

            base.FillDataSet(ref dsID, "CountOldCartsInGioHang");
            return dsID;
        }
        catch (System.Exception e)
        {
            throw new Exception(e.Message, e.InnerException);
        }
        finally
        {
            base.CleanUpObject();
        }
    }

    public Boolean DeleteOldCartsFromGioHang(int days)
    {
        try
        {
            base.sql = "usp_DeleteOldCartsFromGioHang";
            base.InitializeCommand();

            base.AddParameter("@Days",
            SqlDbType.Int, 10, days);

            return ExecuteStoredProcedure();
        }
        catch (System.Exception ExceptionErr)
        {
            throw new System.Exception(ExceptionErr.Message,
            ExceptionErr.InnerException);
        }
    }
}
